﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using ChiDao.Component.Utils;
using ChiDao.Business.Models.Account;
using ChiDao.Site.Models;

using Webdiyer.WebControls.Mvc;
using ChiDao.Business.Service;
using ChiDao.Business.Service.AttachAttribute;

namespace ChiDao.Site.Web.Controllers
{
    [Export]
    public class HomeController : BaseController
    {
        [Import]
        public IAccountService AccountContract { get; set; }

        [WebPage]
        public ActionResult Index(int? id)
        {
            int pageIndex = id ?? 1;
            const int pageSize = 20;
            PropertySortCondition[] sortConditions = new[] { new PropertySortCondition("Id") };
            int total;

            var list = from m in AccountContract.Persons
                       select new { m.UserName, m.NickName };

            var memberViews = AccountContract.Persons.Where<Person, Guid>(m => true, pageIndex, pageSize, out total, sortConditions).Select(m => new PersonView
            {
                UserName = m.UserName,
                NickName = m.NickName,
                Email = m.Email,
                IsDeleted = m.IsDeleted,
                AddDate = m.AddDate,
                LoginLogCount = m.LoginLogs.Count,
                //RoleNames = m.Roles.Select(n => n.RoleName)
            });
            PagedList<PersonView> model = new PagedList<PersonView>(memberViews, pageIndex, pageSize, total);
            return View(model);
        }

        public ActionResult Button()
        {
            return View();
        }
    }
}
